import torch
import numpy as np
import sys
sys.path.append("..")
import os
print(os.getcwd())  # 获取当前工作目录
import d2lzh_pytorch as d2l

#设置小批量并加载数据集
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)

#定义模型参数
num_inputs, num_outputs, num_hiddens = 784, 10, 256
W1 = torch.tensor(np.random.normal(0, 0.01, (num_inputs, num_hiddens)), dtype=torch.float)
b1 = torch.zeros(num_hiddens, dtype=torch.float)
W2 = torch.tensor(np.random.normal(0, 0.01, (num_hiddens, num_outputs)), dtype=torch.float)
b2 = torch.zeros(num_outputs, dtype=torch.float)
params = [W1, b1, W2, b2]
for param in params:
    param.requires_grad_(requires_grad=True)

#定义激活函数，使用max方法实现
def relu(X):
    return torch.max(input=X, other=torch.tensor(0.0))


#定义多层感知机模型
def net(X):
    X = X.view((-1, num_inputs))
    H = relu(torch.matmul(X, W1) + b1)
    return torch.matmul(H, W2) + b2

#损失函数
loss = torch.nn.CrossEntropyLoss()


#训练模型
num_epochs, lr = 5, 100.0
d2l.train_softmax(net, train_iter, test_iter, loss, num_epochs,batch_size, params, lr)